<?php
class  Admin_MenuItemController extends Zend_Controller_Action{
	public function init(){
		
	}
	
	public function indexAction(){
		$menu = $this->_request->getParam('menu');
		$mdlMenu = new Model_Menu();
		$mdlMenuItem = new Model_MenuItem();
		$this->view->menu = $mdlMenu->find($menu)->current();
		$this->view->items = $mdlMenuItem->getItemsByMenu($menu);
	}
	
	public function addAction(){
		$menu = $this->_request->getParam('menu');
		$mdlMenu = new Model_Menu();
		$this->view->menu = $mdlMenu->find($menu)->current();
		$frmMenuItem = new Form_MenuItem();
		if($this->_request->isPost()){
			if($frmMenuItem->isValid($_POST)){
				$data = $frmMenuItem->getValues();
				$mdlMenuItem = new Model_MenuItem();
				$mdlMenuItem->addItem($data['menu_id'], $data['label'], $data['page_id'], $data['link']);
				$this->_request->setParam('menu', $data['menu_id']);
				$this->_forward('index');
			}
		}
		$frmMenuItem->populate(array('menu_id' => $menu));
		$this->view->form = $frmMenuItem;
	}
	
	public function moveAction(){
		$id = $this->_request->getParam('id');
		$direction = $this->_request->getParam('direction');
		$mdlMenuItem = new Model_MenuItem();
		$menuItem = $mdlMenuItem->find($id)->current();
		if($direction == 'up'){
			$mdlMenuItem->moveUp($id);
		}elseif ($direction == 'down'){
			$mdlMenuItem->moveDown($id);
		}
		$this->_request->setParam('menu', $menuItem->menu_id);
		$this->_forward('index');
	}
	
	public function updateAction(){
		$id = $this->_request->getParam('id');
		$mdlmenuItem = new Model_MenuItem();
		$currentMenuItem = $mdlmenuItem->find($id)->current();
		$mdlMenu = new Model_Menu();
		$this->view-menu=$mdlMenu->find($currentMenuItem->menu_id)->current();
		
		$frmMenuItem = new Form_MenuItem();
		$frmMenuItem->setAction('/MenuItem/update');
		if($this->_request->isPost()){
			if($frmMenuItem->isValid($_POST)){
				$data = $frmMenuItem->getValues();
				$mdlmenuItem->updateItem($data['id'], $data['label'], $data['page_id'], $data['link']);
				$this->_request->setParam('menu', $data['menu_id']);
				return $this->_forward('index');
			}
		}else{
			$frmMenuItem->populate($currentMenuItem->toArray());
		}
		$this->view->form = $frmMenuItem;
	}
	
	public function deleteAction(){
		$id = $this->_request->getParams('id');
		$mdlMenuItem = new Model_MenuItem();
		$currentMenuItem = $mdlMenuItem->find($id)->current();
		$mdlMenuItem->deleteItem($id);
		$this->_request->setParam('menu', $currentMenuItem->menu_id);
		$this->_forward('index');
	}
}
?>